package com.hejielife.balking1;

import java.io.IOException;
import java.io.FileWriter;
import java.io.Writer;
/**
 * 2019年8月2日 下午1:19:38
 * @author hejielife
 * @despribe Data
 */
public class Data {
	private String filename; // file name
	private String content; // content
	private boolean changed; // data changed?

	public Data(String filename, String content) {
		this.filename = filename;
		this.content = content;
		this.changed = true;
	}

	// change content
	public synchronized void change(String newContent) {
		content = newContent;
		changed = true;
	}

	// save content
	public synchronized void save() throws IOException {
		if (!changed) {
			System.out.println(Thread.currentThread().getName() + " balks");
			return;
		}
		doSave();
		changed = false;
	}

	// do save
	private void doSave() throws IOException {
		System.out.println(Thread.currentThread().getName() + " calls doSave, content = " + content);
	}
}
